<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Users extends CI_Model {

    function __construct(){
        parent::__construct();
        $this->load->database();
    }
    
    //INSERTING DATA//
    
    function insert($input){
       $this->db->set('name',$input['name']);
       $this->db->set('username',$input['username']);
       $this->db->set('password',$input['password']);
       $this->db->set('role',$input['role'],FALSE);
       $this->db->insert('user');
    }
    
    //SELECTING DATA//
    
    function check_login($input){
        $this->db->where('username',$input['username']);
        $this->db->where('password',md5($input['password']));
        $query = $this->db->get('user');
        return ($query->num_rows() > 0) ? true : false;
    }
    
    function get_user($atribute,$value){
        $this->db->where($atribute,$value);
        $query = $this->db->get('user');
        return ($query->num_rows() > 0) ? $query : null;
    }
    
    function get_user_by_id($id){
        $this->db->where('user_id',$username);
        $query = $this->db->get('user');
        return ($query->num_rows() > 0) ? $query->first_row() : null;
    }
    
    function get_user_by_username($username){
        $this->db->where('username',$username);
        $query = $this->db->get('user');
        return ($query->num_rows() > 0) ? $query->first_row() : null;
    }
    
    function get_all(){
        $query = $this->db->get('user');
        return $query;
    }
    
    function get_atribute_by_id($atribute,$id){
        $query = $this->get_user('user_id', $id);
        $result = $query->first_row();
        return $result->$atribute;
    }
    
    //UPDATING DATA//
    
    function update_by_id($id,$input){
        $this->db->where('user_id', $id);
        $this->db->update('user', $input);
    }
    
    //DELETING DATA//
    
    function delete_by_id($id){
        $this->db->where('user_id', $id);
        $this->db->delete('user');
    }
}